package com.gzdh.tang.modu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.modu.entity.Addr;
import com.gzdh.tang.modu.entity.Bmzl;
import com.gzdh.tang.modu.entity.Zweekmenubak;
import com.gzdh.tang.modu.model.AddrTree;
import com.gzdh.tang.modu.service.BmzlService;
import com.gzdh.tang.modu.service.TangAddrService;
import com.gzdh.tang.modu.service.ZweekmenubakService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping(value = "/tang/info")
public class StoresController {
    @Autowired
    TangAddrService tangAddrService;
    @Autowired
    BmzlService bmzlService;
    @Autowired
    ZweekmenubakService zweekmenubakService;

    @GetMapping("getAddr")
    public Result<Object> getAddr(@RequestParam(name = "operaId", required = false) String operaId) {
        Result<Object> result = new Result<>();

        List<Addr> addrList = new ArrayList<>();

        if (operaId == null || "".equals(operaId)) {
            addrList = tangAddrService.list(new QueryWrapper<Addr>().lambda().ge(Addr::getAddrNo, 1));
        } else {
            addrList = tangAddrService.getAddrByOpera(operaId);
        }
        List<AddrTree> addrTreeList = new ArrayList<>();
        getTreeList(addrTreeList, addrList, null);
        result.setResult(addrTreeList);
        return result;
    }

    private void getTreeList(List<AddrTree> treeList, List<Addr> metaList,
                             AddrTree temp) {
        for (Addr permission : metaList) {
            int tempPid = permission.getParentId();
            AddrTree tree = new AddrTree();
            tree.setId(permission.getAddrNo());
            tree.setAddrName(permission.getAddrName());
            tree.setChildren(new ArrayList<>());
            if (temp == null && tempPid == 0) {
                treeList.add(tree);
                if (true) {
                    getTreeList(treeList, metaList, tree);
                }
            } else if (temp != null && tree != null && tempPid == temp.getId()) {
                temp.getChildren().add(tree);
//                if (true) {
//                    getTreeList(treeList, metaList, tree);
//                }
            }
        }
    }


    @GetMapping("getBmzl")
    public Result<Object> getBmzl() {
        Result<Object> result = new Result<>();
        List<Addr> addrList = tangAddrService.list();
        Map<Integer, List<Bmzl>> map = new HashMap<>();
        //遍历Addr获取所有灶类
        for (Addr goo : addrList) {
            try {
                List<Bmzl> bmzlList = bmzlService.findBmzlList(goo.getAddrNo());
                map.put(goo.getAddrNo(), bmzlList);
            } catch (Exception e) {
                log.info("当前地点【" + goo.getAddrNo() + "---" + goo.getAddrName() + "】，尚未找到BMZL");
            }
        }
        result.setResult(map);
        return result;
    }


    @GetMapping("getZWeekMenuBak")
    public Result<Object> getZWeekMenuBak() {
        Result<Object> result = new Result<>();
        List<Addr> addrList = tangAddrService.list();
        Map<Integer, List<Zweekmenubak>> map = new HashMap<>();
        for (Addr goo : addrList) {
            try {
                List<Zweekmenubak> zweekmenubakList = zweekmenubakService.findZweekMenuBak(goo.getAddrNo());
                map.put(goo.getAddrNo(), zweekmenubakList);
            } catch (Exception e) {
                log.info("当前地点【" + goo.getAddrNo() + "---" + goo.getAddrName() + "】，尚未找到菜谱");
            }
        }
        result.setResult(map);
        return result;
    }



}
